// pages/leaderData/leaderData.js
var common = require("../../common/common.js").common;

var colorParse = require("../../common/color.js");

var {$author: $author} = require("../../components/base/authorization.js");

Page({
    /**
   * 页面的初始数据
   */
    data: {
        alert: 0,
        search_con: "",
        chooseClass: 1,
        page: 1,
        order: [],
        date_check: false,
        dateMonth: "历史",
        goodsListStyle: {
            style: 4,
            styleConfig: {
                backgroundColor: "#fff"
            }
        },
        plat_time: [ {
            text: "今日",
            date: "TdateFormate",
            type: 1
        }, {
            text: "昨日",
            date: "YdateFormate",
            type: 2
        }, {
            text: "本周",
            date: "WeekFormate",
            type: 3
        }, {
            text: "本月",
            date: "MonthFormate",
            type: 4
        } ]
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        wx.hideShareMenu();
        let user_id = options.user_id;
        this.setData({
            user_id: user_id
        });
        this.getBase();
        this.getLeaderUnderDetail();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {
        let that = this;
        that.setData({
            page: 1,
            order: []
        });
        that.getLeaderUnderDetail();
        wx.stopPullDownRefresh();
    },
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {
        let that = this;
        that.getLeaderUnderDetail();
    },
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    //全局基础配置信息
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                var goodsListStyle = that.data.goodsListStyle;
                var copyText = [];
                if (res.extra.copyright_msg) {
                    var copy = res.extra.copyright_msg;
                    if (copy.indexOf(",") != -1) {
                        copyText = copy.split(",");
                    } else {
                        copyText[0] = copy;
                    }
                }
                var rgba = colorParse.hexToRgb(res.extra.color_main, .8);
                that.setData({
                    goodsListStyle: {
                        ...goodsListStyle,
                        mainColor: res.extra.color_main
                    },
                    color_main: res.extra.color_main,
                    color_main2: rgba,
                    color_minor: res.extra.color_minor,
                    apptitle: res.extra.title,
                    style: res.extra.style,
                    leader_change: res.extra.l_change,
                    phone: res.extra.phone,
                    copyImg: res.extra.copyright_img,
                    copyText: copyText,
                    l_supply: res.extra.l_supply,
                    rp_name: res.extra.rp_name,
                    leaderName: res.extra.leader_title ? res.extra.leader_title : "团长",
                    leader_address_on: res.extra.leader_address_on,
                    vip_card_on: res.extra.vip_card_on,
                    title: "旗下" + res.extra.leader_title ? res.extra.leader_title : "团长",
                    new_member_audit: res.extra.new_member_audit,
                    userAuditStatus: res.extra.userAuditStatus ? res.extra.userAuditStatus : 2
                });
                //处理模块开关
                                var features = common.getModulesSwich(res.extra.features);
                //处理数据格式
                                that.setData({
                    features: features
                });
                wx.setNavigationBarTitle({
                    title: "旗下" + res.extra.leader_title ? res.extra.leader_title : "团长"
                });
            }
        });
    },
    bindDateChange(e) {
        this.setData({
            dateMonth: e.detail.value,
            date_check: false,
            chooseClass: "5",
            page: 1,
            order: []
        });
        this.getLeaderUnderDetail();
    },
    bindDateMonthChange(e) {
        this.setData({
            dateMonth: e.detail.value,
            date_check: false,
            chooseClass: "6",
            page: 1,
            order: []
        });
        this.getLeaderUnderDetail();
    },
    dateCheckChange() {
        this.setData({
            date_check: !this.data.date_check
        });
    },
    queryData: function(e) {
        let that = this;
        that.setData({
            chooseClass: e.currentTarget.dataset.type,
            page: 1,
            order: []
        });
        that.getLeaderUnderDetail();
    },
    getLeaderUnderDetail: function() {
        let that = this;
        var openid = wx.getStorageSync("user").openid;
        var params = {
            openid: openid,
            type: that.data.chooseClass,
            user_id: that.data.user_id,
            page: that.data.page
        };
        if (that.data.chooseClass == "5" || that.data.chooseClass == "6") {
            params["time"] = that.data.dateMonth;
        }
        common.request({
            endpoint: "My/leader_under/detail",
            method: "post",
            data: params,
            header: {
                "content-type": "application/json"
            },
            success: res => {
                var data = that.data.order;
                var newData = that.orderList(res.extra.order_info);
                let order = that.data.page == 1 ? newData : data.concat(newData);
                if (res.extra.order_info.length > 0) {
                    that.setData({
                        plat_data: res.extra.general,
                        order: order
                    });
                    that.data.page = that.data.page + 1;
                } else {
                    that.setData({
                        plat_data: res.extra.general
                    });
                }
            }
        });
    },
    orderList(data) {
        if (data || data.length > 0) {
            data.forEach((item, index) => {
                var newProArry = [];
                var deliveryCost = [];
                var aftermarket_num = 0;
                //可售后子单数量
                                var delivery1_num = 0;
                //自提订单数量
                                var delivery2_num = 0;
                //团长配送订单数量
                                var delivery3_num = 0;
                //平台配送订单数量
                                var status4_num = 0;
                //待取货订单数量
                // 拼运费字符串
                                if (item.esb_delivery && item.esb_delivery != "0.00") {
                    deliveryCost.push("平台配送费：￥" + item.esb_delivery);
                }
                if (item.express_delivery && item.express_delivery != "0.00") {
                    deliveryCost.push("快递配送费：￥" + item.express_delivery);
                }
                if (item.leader_delivery && item.leader_delivery != "0.00") {
                    deliveryCost.push("团长配送费：￥" + item.leader_delivery);
                }
                item.item.forEach((proInfo, proIndx) => {
                    //统计可售后子单数量
                    if (proInfo.aftermarket_overtime == 1) {
                        aftermarket_num++;
                    }
                    if (proInfo.order_status == "4") {
                        status4_num++;
                    }
                    switch (proInfo.delivery_channel) {
                      case "1":
                        delivery1_num++;
                        break;

                      case "2":
                        delivery2_num++;
                        break;

                      case "3":
                        delivery3_num++;
                        break;
                    }
                    var pro = {
                        id: proInfo.id,
                        //订单id，
                        proId: proInfo.pro_id,
                        //商品id
                        icon: proInfo.icon,
                        //商品缩略图
                        proName: proInfo.alias_name,
                        //商品名称
                        spec: proInfo.product_info,
                        //规格
                        money_refund: proInfo.money_refund,
                        //是否是满减商品
                        price: proInfo.price,
                        //商品单价
                        num: proInfo.num,
                        //数量
                        status: proInfo.status,
                        //售后状态，-1——仅退款，-2—— 退货退款，-3退货换货
                        order_status: proInfo.order_status,
                        //订单状态
                        isExpressPro: proInfo.delivery_channel == 4 ? 1 : 0,
                        //是否是快递商品，1是，2否
                        order_status: proInfo.order_status,
                        //订单状态
                        payment_channel: item.payment_channel,
                        //支付方式
                        leader_brokerage: proInfo.leader_brokerage,
                        //团长佣金
                        recommend_brokerage: proInfo.recommend_brokerage
                    };
                    newProArry.push(pro);
                });
                var newOrder = {
                    totalNum: item.allNum,
                    item: newProArry
                };
                item.goodsListData = newOrder;
                item.deliveryCost = "(" + (deliveryCost.length > 0 ? deliveryCost.join(",") : "免配送费") + ")";
                item.aftermarket_num = aftermarket_num;
                item.delivery1_num = delivery1_num;
                item.delivery2_num = delivery2_num;
                item.delivery3_num = delivery3_num;
                item.status4_num = status4_num;
            });
            return data;
        }
    }
});